<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\helper\Time;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class ReserveStatistics extends Backend
{
    
    /**
     * TracePosition模型对象
     * @var \app\admin\model\TracePosition
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ReserveStatistics;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    public function index()
    {
        $filter = json_decode(input('filter'),true);
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            $type = $this->request->get('type');
            $paylist = $createlist = [];

            switch ($_GET['type']) {
                case '1':
                //默认是今天的
                $today = Time::today();
                for ($i = 0; $i < 24; $i++)
                {
                    $every = $i.":00";
                    //根据天统计数量
                    $todayStr = date("Y-m-d",$today[0]);
                    //根据天统计数量
                    $begin_time = $todayStr." ".$i.":00";

                    $e = $i+1;
                    $end_time = $todayStr." ".$e.":00";

                    $whereTime = ' add_time between '.strtotime($begin_time).' and '.strtotime($end_time);
                    // $total_count = (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($whereTime) -> count();
                    $paylist[$every] = $this->getCount($whereTime);
                }
                break;
                
                case '2':
                    //选择是昨天
                    //echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";      
                    $yesterday = date("Y-m-d",strtotime("-1 day"));
                    for ($i = 0; $i < 24; $i++)
                    {
                        $yester = $i.":00";
                        //根据昨天统计数量
                        $begin_time = $yesterday." ".$i.":00";
                        $e = $i+1;
                        $end_time = $yesterday." ".$e.":00";
                        $whereTime = ' add_time between '.strtotime($begin_time).' and '.strtotime($end_time);
                        //$total_count = (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($whereTime) -> count();
                        $paylist[$yester] = $this->getCount($whereTime);
                    }
                break;

                case '7':
                    //七天
                    $seventtime = \fast\Date::unixtime('day', -7);
                    for ($i = 0; $i < 7; $i++)
                    {
                        //根据天统计数量
                        $day = date("Y-m-d", $seventtime + ($i * 86400));
                        //某一天开始
                        $day_begin = date("Y-m-d 0:0:0", $seventtime + ($i * 86400));
                        //echo $day_begin."####";
                        //某一天结束
                        $day_end = date("Y-m-d 24:0:0", $seventtime + ($i * 86400));
                        //echo $day_end."<br/>";
                        $dayfrom = strtotime($day_begin);
                        $dayto = strtotime($day_end);
                        $whereTime['add_time'] = ['between', "$dayfrom, $dayto"];
                        // $total_count = (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($whereTime) -> count();
                        $paylist[$day] = $this->getCount($whereTime);
                    }
                break;

                case '30':
                    //最近30天
                    $seventtime = \fast\Date::unixtime('day', -30);
                    for ($i = 0; $i < 30; $i++)
                    {
                        //根据天统计数量
                        $day = date("Y-m-d", $seventtime + ($i * 86400));
                        //某一天开始
                        $day_begin = date("Y-m-d 0:0:0", $seventtime + ($i * 86400));
                        //echo $day_begin."####";
                        //某一天结束
                        $day_end = date("Y-m-d 24:0:0", $seventtime + ($i * 86400));
                        //echo $day_end."<br/>";
                        $dayfrom = strtotime($day_begin);
                        $dayto = strtotime($day_end);

                        $whereTime['add_time'] = ['between', "$dayfrom, $dayto"];

                        // $total_count = (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($whereTime) -> count();
                        $paylist[$day] = $this->getCount($whereTime);
                    }
                break; 
                
                case '31':
                    //获取上个月
                    $shang = date('Y-m-t',strtotime('-1 month'));
                    $shangyear =date('Y-m',strtotime($shang)); //年月
                    $shangtime =date('d',strtotime($shang));
                    for ($i = 1; $i <=$shangtime; $i++)
                    {
                        //根据每天天统计数量
                        $day = $shangyear.'-'.$i;
                        //某一天开始
                        $day_begin = date("Y-m-d 0:0:0", strtotime($day));
                        //某一天结束
                        $day_end = date("Y-m-d 24:0:0",strtotime($day));
                        // //echo $day_end."<br/>";
                        $dayfrom = strtotime($day_begin);
                        $dayto = strtotime($day_end);

                        $whereTime['add_time'] = ['between', "$dayfrom, $dayto"];

                        // $total_count = (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($whereTime) -> count();
                        $paylist[$day] = $this->getCount($whereTime);
                    }
                break;

                case '32':
                    //获取本月
                    //今天第几天
                    $todayyear =date('Y-m'); //年月
                    $todaytime =date('d');
                    for ($i = 1; $i <=$todaytime; $i++)
                    {
                        //根据每天天统计数量
                        $day = $todayyear.'-'.$i;
                        //某一天开始
                        $day_begin = date("Y-m-d 0:0:0", strtotime($day));
                        //某一天结束
                        $day_end = date("Y-m-d 24:0:0",strtotime($day));
                        // //echo $day_end."<br/>";
                        $dayfrom = strtotime($day_begin);
                        $dayto = strtotime($day_end);

                        $whereTime['add_time'] = ['between', "$dayfrom, $dayto"];

                        // $total_count = (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($whereTime) -> count();
                        $paylist[$day] =  $this->getCount($whereTime);
                    }
                break;
                default:
                    //默认是今天的
                    $today = Time::today();
                    for ($i = 0; $i < 24; $i++)
                    {
                        $every = $i.":00";
                        // echo $i
                        //根据天统计数量
                        $todayStr = date("Y-m-d",$today[0]);
                        //根据天统计数量
                        $begin_time = $todayStr." ".$i.":00";

                        $e = $i+1;
                        $end_time = $todayStr." ".$e.":00";

                        $whereTime = ' add_time between '.strtotime($begin_time).' and '.strtotime($end_time);
                        //$total_count = (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($whereTime) -> count();
                        $paylist[$every] = $this->getCount($whereTime);
                    } 
                break;
            }

            $datalist =array();
            $datalist['column']= array_keys($paylist);
            $datalist['paydata']= array_values($paylist);

            $this->success('', null, $datalist);
            die();
        }
        //exit();
        $paylist = $createlist = [];

        //默认是今天的
        $today = Time::today();
        for ($i = 0; $i < 24; $i++)
        {
            $every = $i.":00";
            // echo $i
            //根据天统计数量
            $todayStr = date("Y-m-d",$today[0]);
            //根据天统计数量
            $begin_time = $todayStr." ".$i.":00";

            $e = $i+1;
            $end_time = $todayStr." ".$e.":00";

            $whereTime = ' add_time between '.strtotime($begin_time).' and '.strtotime($end_time);
            //$total_count = (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($whereTime) -> count();
            $paylist[$every] = $this->getCount($whereTime);
        }  

        // echo "<pre>";
        // var_dump(date('Y-m-d',$seventtime));
        // echo "</pre>";
        //exit();
        //统计图信息
        $this->view->assign([
            'paylist'=>$paylist,
        ]);        

        //统计
        $where_['id'] = 1;
        $reserve = (new \app\admin\model\ReserveSetting()) -> alias('rs')->where($where_) -> find();

        //预约成功人数
        $where ['states']=1;
        $allcount = $this->getCount($where);

        //预约过期人数
        $whered['states']=2;
        $allcounted = $this->getCount($whered);

        //进入图书馆人数
        $where_into['into_states']=1; //进入
        $where_into['leave_states']=0;//未离开
        $into_count = $this->getCount($where_into);

        //已离开图书馆人数
        $where_leave['leave_states']=1;
        $leave_count = $this->getCount($where_leave);

        if($reserve['reserve_number']-$reserve['reserved_number']<0)
        {
            $yi_yuyued = 0;
        }else{
            $yi_yuyued = $reserve['reserve_number']-$reserve['reserved_number'];
        }

        $this -> assign('allcount',$allcount);
        $this -> assign('allcounted',$allcounted);
        $this -> assign('yi_yuyued',$yi_yuyued);        
        $this -> assign('reserve',$reserve);
        $this -> assign('into_count',$into_count);
        $this -> assign('leave_count',$leave_count);


        return $this->view->fetch();
    }

    //统计数量
    public function getCount($where)
    {
        return (new \app\admin\model\ReserveRecord()) -> alias('rr')->where($where) -> count();
    }


}
